--[[
* MapLib 2.0
* File: <AddOn>\MapLib\MapLib_Options.lua
* $Revision: 132 $
* $Date: 2007-01-06 13:08:56 +0000 (Sat, 06 Jan 2007) $
* 
* Copyright (c) 2006-2007, Mery (Moritz Metelmann)
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* 
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*    * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]--

local vmajor, vminor = "MapLib-2.0", tonumber(string.sub("$Revision: 132 $", 12, -3))

-- Version check: Only one major instance of MapLib can be installed, if another already is: abort.
if (MapLib and MapLib.Options and (MapLib.Options.vmajor ~= vmajor or MapLib.Options.vminor >= vminor)) then return; end;

if (not MapLib) then MapLib = {}; end;
if (not MapLib.Options) then MapLib.Options = {}; end;
if (not MapLib.Options.Event) then MapLib.Options.Event = {}; end;

MapLib.Options.vmajor = vmajor;
MapLib.Options.vminor = vminor;

local L = MapLib.Localization;

MapLib.Options.RegisterMenu = function(name, frame, activeButtonTexture, inactiveButtonTexture)
	MapLib.XML.CreateOptionsButton(name, frame, activeButtonTexture, inactiveButtonTexture);
	frame:SetParent(MapLibOptions);
end;

MapLib.Options.TabOnClick = function()
	MapLib.Options.SetTabToID(this:GetID());
end;

MapLib.Options.SetTabToID = function(id)
	for i=1, MapLib.NUM_OPTIONSBUTTONS, 1 do
		if (i == id) then
			-- diable button, set to active
			getglobal("MapLibOptionsTabButton"..i):Disable();
			getglobal("MapLibOptionsTabButton"..i.."TextureInActiveLeft"):Hide();
			getglobal("MapLibOptionsTabButton"..i.."TextureInActiveMiddle"):Hide();
			getglobal("MapLibOptionsTabButton"..i.."TextureInActiveRight"):Hide();
			getglobal("MapLibOptionsTabButton"..i.."TextureLeft"):Show();
			getglobal("MapLibOptionsTabButton"..i.."TextureMiddle"):Show();
			getglobal("MapLibOptionsTabButton"..i.."TextureRight"):Show();
			-- show frame
			local optionsFrame = getglobal("MapLibOptionsTabButton"..i).optionsFrame;
			optionsFrame:Show();
			-- reanchor according to fullscreen or not:
			optionsFrame:SetPoint("TOPLEFT", MapLibOptions, "TOPLEFT", 12, -12);
		else
			-- enable button, set to inactive
			getglobal("MapLibOptionsTabButton"..i):Enable();
			getglobal("MapLibOptionsTabButton"..i.."TextureInActiveLeft"):Show();
			getglobal("MapLibOptionsTabButton"..i.."TextureInActiveMiddle"):Show();
			getglobal("MapLibOptionsTabButton"..i.."TextureInActiveRight"):Show();
			getglobal("MapLibOptionsTabButton"..i.."TextureLeft"):Hide();
			getglobal("MapLibOptionsTabButton"..i.."TextureMiddle"):Hide();
			getglobal("MapLibOptionsTabButton"..i.."TextureRight"):Hide();
			-- hide frame
			getglobal("MapLibOptionsTabButton"..i).optionsFrame:Hide();
		end;
	end;
	MapLib.Options.currentTab = id;
end;

MapLib.Options.OnShow = function()
	MapLib.Block.Request("MapNotesOptions", function() end, MapLib.FRAMELEVEL_DROPDOWNMENU-1);
	-- if sidemenu is shown hide it and remember it
	
end;

MapLib.Options.OnHide = function()
	MapLib.Block.Release("MapNotesOptions");
	-- restore side menu if nessesary
	
end;

MapLib.Options.OnCancelClicked = function()
	MapLib.EventHandler.TriggerEvent("MAPLIB_OPTIONS_RESET");
	MapLibOptions:Hide();
end;


MapLib.Options.OnSaveClicked = function()
	MapLib.EventHandler.TriggerEvent("MAPLIB_OPTIONS_SAVE");
	MapLibOptions:Hide();
end;

MapLib.Options.OnResetClicked = function()
	MapLib.EventHandler.TriggerEvent("MAPLIB_OPTIONS_RESET");
end;

MapLib.Options.OnDefaultClicked = function()
	MapLib.EventHandler.TriggerEvent("MAPLIB_OPTIONS_DEFAULT");
end;

MapLib.Options.RegisterMenu(L["General"], MapLib.XML.CreateMapLibOptionsFrame());
MapLib.XML.CreateMapLibOptionsFrame = nil;
MapLibOptionsCancelButton:SetScript("OnClick", MapLib.Options.OnCancelClicked);
MapLibOptionsSaveButton:SetScript("OnClick", MapLib.Options.OnSaveClicked);
MapLibOptionsResetButton:SetScript("OnClick", MapLib.Options.OnResetClicked);
MapLibOptionsDefaultButton:SetScript("OnClick", MapLib.Options.OnDefaultClicked);
MapLibOptions:SetScript("OnShow", MapLib.Options.OnShow);
MapLibOptions:SetScript("OnHide", MapLib.Options.OnHide);

-- use the events save, reset default
-- manage all other stuff that happens in the options frame
MapLib.Options.MAPLIB_OPTIONS_RESET = function()
	-- Replace World Map
	if (MapLibSavedPerCharacter.Options.replaceWorldMap) then
		MapLibOptionsFrameCheckButton1:SetChecked(true);
	else
		MapLibOptionsFrameCheckButton1:SetChecked(false);
	end;

end;

MapLib.Options.MAPLIB_OPTIONS_DEFAULT = function()
	-- Replace World Map (Default: OFF)
	MapLibOptionsFrameCheckButton1:SetChecked(false);
	
end;

MapLib.Options.MAPLIB_OPTIONS_SAVE = function()
	-- Replace World Map
	MapLibSavedPerCharacter.Options.replaceWorldMap = MapLibOptionsFrameCheckButton1:GetChecked();
	if (MapLibSavedPerCharacter.Options.replaceWorldMap) then
		MapLib.Options.orig_ToggleWorldMap = MapLib.Options.orig_ToggleWorldMap or ToggleWorldMap;
		ToggleWorldMap = MapLib.ToggleMap;
	else
		ToggleWorldMap = MapLib.Options.orig_ToggleWorldMap or ToggleWorldMap;
	end;
end;

MapLib.Options.RestoreSavedValuesOnLoad = function()
	if (not MapLibSavedPerCharacter) then MapLibSavedPerCharacter = {}; end;
	if (not MapLibSavedPerCharacter.Options) then 
		MapLibSavedPerCharacter.Options = {};
		MapLib.Options.MAPLIB_OPTIONS_DEFAULT();
	else
		MapLib.Options.MAPLIB_OPTIONS_RESET();
	end;
	MapLib.Options.MAPLIB_OPTIONS_SAVE();
	MapLibOptions:UnregisterEvent("ADDON_LOADED");
	MapLibOptions:SetScript("OnEvent", nil);
end;
MapLibOptions:RegisterEvent("ADDON_LOADED");
MapLibOptions:SetScript("OnEvent", MapLib.Options.RestoreSavedValuesOnLoad);

MapLib.EventHandler.RegisterEvent("MAPLIB_OPTIONS", "MAPLIB_OPTIONS_RESET", MapLib.Options.MAPLIB_OPTIONS_RESET);
MapLib.EventHandler.RegisterEvent("MAPLIB_OPTIONS", "MAPLIB_OPTIONS_DEFAULT", MapLib.Options.MAPLIB_OPTIONS_DEFAULT);
MapLib.EventHandler.RegisterEvent("MAPLIB_OPTIONS", "MAPLIB_OPTIONS_SAVE", MapLib.Options.MAPLIB_OPTIONS_SAVE);